<?php
    
namespace Liuhelong\LaravelAdmin\Wechat\Jobs;
    
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Log;
use EasyWeChat\Kernel\Messages;
use Liuhelong\LaravelAdmin\Wechat\Models\WechatOffiaccountNew;

class HandleCsMessageSend implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
    
    private $handle_class;
    private $message;
    public $tries = 1;
    /**
     * 创建任务实例
     *
     * @param  Podcast  $podcast
     * @return void
     */
    public function __construct($class_handle,$message)
    {
        $this->class_handle = $class_handle;
        $this->message = $message;
    }
    
    /**
     * 执行任务
     *
     * @param  AudioProcessor  $processor
     * @return void
     */
    public function handle()
    {
        $wechat = app('liuhelong_wechat_service');
	    if(!empty($this->class_handle->cs_messages)){
	        foreach($this->class_handle->cs_messages as $cs_message){
	            switch($cs_message->reply_type){
        	        case('text'):
        	            $message = new Messages\Text($cs_message->reply_content);
        	            break;
        	        case('image'):
        	            $message = new Messages\Image($cs_message->reply_content);
        	            break;
        	        case('voice'):
        	            $message = new Messages\Voice($cs_message->reply_content);
        	            break;
        	        case('video'):
        	            $video = $wechat->handle('material','get',[$cs_message->reply_content]);
        	            $message = new Messages\Video($cs_message->reply_content,[$video['title'],$video['description']]);
        	            break;
        	        // 微信api调整 弃用图文素材 改为已发布图文
        	       // case('mpnews'):
        	       //     $message = new Messages\Media($cs_message->reply_content,'mpnews');
        	       //     break;
        	       case('mpnewsarticle'):
        	            $items = '{
                            "touser":"'.$this->message['FromUserName'].'",
                            "msgtype":"mpnewsarticle", 
                            "mpnewsarticle":
                            {
                                "article_id":"'.($cs_message->reply_content).'"
                            }
                        }';
        	            $message = new Messages\Raw($items);
        	            break;
        	        case('news'):
        	            $new = WechatOffiaccountNew::find($cs_message->reply_content);
        	            $items = [
                            new Messages\NewsItem([
                                'title'       => $new->title,
                                'description' => $new->description,
                                'url'         => $new->url,
                                'image'       => $new->image,
                            ]),
                        ];
        	            $message = new Messages\News($items);
        	            break;
        	        case('miniprogrampage'):
        	            $items = '{
                            "touser":"'.$this->message['FromUserName'].'",
                            "msgtype":"miniprogrampage", 
                            "miniprogrampage":
                            {
                                "title":"'.($cs_message->miniprogrampage_data['title']??'').'",
                                "appid":"'.($cs_message->miniprogrampage_data['appid']??'').'",
                                "pagepath":"'.($cs_message->miniprogrampage_data['pagepath']??'').'",
                                "thumb_media_id":"'.($cs_message->miniprogrampage_data['thumb_media_id']??'').'"
                            }
                        }';
        	            $message = new Messages\Raw($items);
        	            break;
        	    }
        	    $wechat->fluent_handle('customer_service',[
        	        [
        	            'message',
        	            [$message]
	                ],
	                [
        	            'to',
        	            [$this->message['FromUserName']]
	                ],
	                [
        	            'send'
	                ],
    	        ]);
	        }
	    }
	    
    }
}